<template>
  <div class="searchTopPopup" @click.stop="()=>{}">
    <div>
      <div class="search">
        <div>
          <div class="search_main">
            <input type="text" id="ygIndexTopS" ref="input" v-model="kword" />
            <input type="button" class="ygIndexTopSub" />
          </div>
          <div class="ygIndexTopL" @click="$emit('set:show')"></div>
        </div>
      </div>
      <div v-if="!kword">
        <div class="hot">
          <div class="searchHistoryT">热门搜索</div>
          <ul>
            <li v-for="(item, index) in arrValue" :key="index">
              <a @click="setRouter(item)">{{ item }}</a>
            </li>
          </ul>
        </div>
        <div class="history">
          <div class="searchHistory">历史搜索</div>
          <ul class="searchHistoryUl">
            <li>
              <a>百丽</a>
            </li>
          </ul>
        </div>
      </div>
      <div v-else class="search-Hy">
        <ul class="searchHistoryUl">
          <li v-for="(item, index) in kwordArr" :key="index">
            <a @click="setRouter(item.suggest)">
              {{ item.suggest }}
              <span>{{ item.count }}个结果</span>
            </a>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { getKeyword } from "@/api";

export default {
  inject: ["reload"],
  name: "searchTopPopup",
  data() {
    return {
      arrValue: [
        "百丽",
        "热卖冬靴",
        "新款单鞋",
        "他她",
        "马丁靴",
        "阿迪",
        "老爹鞋",
        "跑步鞋",
        "天美意",
        "休闲鞋",
        "小白鞋",
        "思加图",
        "卫衣"
      ],
      kword: "",
      kwordArr: []
    };
  },
  methods: {
    setRouter(kword) {
      if (this.$route.query.kword !== kword) {
        this.$refs.input.value = kword;
        this.$router.push({
          path: "/ProductList",
          query: { kword: this.$refs.input.value, region: "kword" }
        });
      }
      this.reload();
    }
  },
  watch: {
    kword(to) {
      getKeyword(to).then(res => {
        this.kwordArr = res;
      });
    }
  }
};
</script>
<style scoped>
.searchTopPopup {
  width: 100%;
  height: 100%;
  background: #fff;
}

.search {
  padding: 0.2rem 0;
  position: relative;
  width: 100%;
  max-width: 12.8rem;
  border-bottom: 0.02rem solid #ccc;
}
.search > div {
  margin-left: 0.76rem;
  margin-right: 0.2rem;
  width: auto;
}

.search_main {
  margin: 0 auto;
  position: relative;
}

#ygIndexTopS {
  outline: 0;
  display: block;
  width: 100%;
  position: relative;
  border-radius: 0.06rem;
  border: 0.02rem solid #ccc;
  padding: 0 0.6rem 0 0.2rem;
  box-sizing: border-box;
  font-size: 0.28rem;
  height: 0.68rem;
  padding-right: 1.54rem;
  background-image: none;
  background-color: #fff;
}

.ygIndexTopSub {
  border: 0;
  outline: 0;
  content: "";
  position: absolute;
  border-radius: 0 0.06rem 0.06rem 0;
  top: 0.02rem;
  right: 0.02rem;
  background: url("../../assets/icon/ico-srch.png") center center no-repeat;
  border-left: 1px solid #ccc;
  height: 32px;
  width: 43px;
  background-size: 20px auto;
  background-color: #f1f1f1;
}

.ygIndexTopL {
  height: 0.56rem;
  position: absolute;
  left: 0;
  background: url("../../assets/icon/ico-close.png") center center no-repeat;
  background-size: 0.3rem auto;
  width: 0.76rem;
  top: 0.26rem;
}

.hot {
  background: #fff;
  padding: 0 0.14rem;
}

.searchHistoryT {
  border: none;
  margin-left: 0.1rem;
  background: url("../../assets/icon/hot.png") center left no-repeat;
  background-size: 0.24rem auto;
  color: #666;
  font-size: 0.28rem;
  padding: 0.24rem 0;
  padding-left: 0.4rem;
  line-height: 1;
  text-align: left;
}

.hot ul {
  overflow: hidden;
}

.hot li {
  float: left;
  margin: 0 0.1rem 0.2rem;
}

.hot li a {
  display: block;
  color: #666;
  padding: 0.06rem 0.24rem;
  background: #eee;
  border-radius: 0.1rem;
  line-height: 0.48rem;
}

.history {
  background: #fff;
  padding: 0 0.24rem;
  border-top: 0.16rem solid #eee;
  padding-bottom: 0.36rem;
}

.searchHistory {
  border-bottom: 0.02rem solid #ddd;
  color: #a0a0a0;
  padding: 0.24rem 0;
  line-height: 1.5;
  text-align: left;
}

.searchHistoryUl {
  margin-bottom: 0.36rem;
}

.searchHistoryUl li {
  border-bottom: 0.02rem solid #ddd;
  line-height: 1.5;
  text-align: left;
}

.searchHistoryUl li a {
  display: block;
  padding: 0.3rem 0;
  line-height: 0.34rem;
  white-space: nowrap;
  text-overflow: ellipsis;
  position: relative;
  overflow: hidden;
  color: #666;
}

.search-Hy {
  padding: 0 0.24rem 0;
}

.searchHistoryUl span {
  float: right;
  color: #a0a0a0;
}
</style>